<template>
  <div class="detail">
    <div class="header-box">
      <div class="header">
        <img
          class="return"
          @click="goreturn"
          src="https://www.pmdaniu.com/storages/101027/ba18b5f48ada3273e098f37e8715880e-30118/images/%E5%95%86%E5%93%81%E8%AF%A6%E6%83%85/u1715.png"
          alt=""
        />
        <div class="title">
          <div
            v-for="(item, index) in titlelist"
            :key="index"
            :class="{ selected: active == index }"
          >
            {{ item }}
          </div>
        </div>
      </div>
    </div>

    <div class="banner">
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
        <van-swipe-item v-for="item in bannerlist" :key="item.id">
          <img :src="item.src" alt="" />
        </van-swipe-item>
      </van-swipe>
    </div>
    <div class="goods-detail">
      <div class="spand">
        <div class="spand-money">￥{{ goodslist.goodprice }}</div>
        <div class="spand-msg">品牌直采</div>
      </div>
      <div class="revenue">
        <span class="revenue-msg">税</span>
        <span class="revenue-spand">{{ revenue }}</span>
      </div>
      <div class="goods-msg">
        {{ goodslist.goodmsg }}
      </div>
      <div class="goods-nice">·跨境商品·品牌授权·品牌直采</div>
    </div>
    <div class="kong"></div>
    <div class="yun">
      <div class="p">
        <div class="left">运费</div>
        <div class="right">免运费</div>
      </div>
      <div class="p">
        <div class="left">说明</div>
        <div class="right">商品税费 | 假一赔十 | 7天无理由退货</div>
      </div>
      <div class="p">
        <div class="left">参数</div>
        <div class="right">淡金色 [ 10cm ]</div>
      </div>
    </div>
    <div class="kong"></div>
    <div class="brand">
      <div class="title">品牌信息</div>
      <div class="p">
        <div class="left">品牌名</div>
        <div class="right">BDM STUDIO</div>
      </div>
      <div class="p">
        <div class="left">原产地</div>
        <div class="right">法国巴黎</div>
      </div>
      <div class="p">
        <div class="left">风格</div>
        <div class="right">欧美</div>
      </div>
    </div>
    <div class="kong"></div>
    <div class="goods-detaiByImg">
      <div class="title">商品详情</div>
      <div class="img-box">
        <img v-for="item in detaillist" :key="item.id" :src="item.src" alt="" />
      </div>
    </div>
    <!--  -->
    <div class="foryoutitle">相似推荐</div>
    <div class="foryou-content">
      <div
        class="foryou-item"
        @click="getDetail(item.id)"
        v-for="item in alllist"
        :key="item.id"
      >
        <div class="imgbox">
          <img :src="item.src" alt="" />
        </div>
        <div class="msg van-multi-ellipsis--l2">
          {{ item.goodmsg }}
        </div>
        <div class="price">￥{{ item.goodprice }}</div>
      </div>
    </div>
    <!--  -->
    <div class="tofooter">已经到底啦</div>
    <div class="footer">
      <van-goods-action>
        <van-goods-action-icon icon="shop-o" text="首页" dot @click="tohome" />

        <van-goods-action-icon
          @click="toShoppingcart"
          icon="cart-o"
          text="购物车"
          :badge="cartnum"
        />

        <van-goods-action-button
          color="#be99ff"
          type="warning"
          @click="addcart()"
          text="加入购物车"
        />
        <van-goods-action-button
          color="#7232dd"
          type="danger"
          text="立即购买"
        />
      </van-goods-action>
    </div>
  </div>
</template>

<style lang="scss" scoped>
$main: #c1ab96;
* {
  margin: 0;
  padding: 0;
}
.kong {
  height: 12px;
  width: 100%;
  background-color: #eae9e5;
}
.my-swipe .van-swipe-item {
  color: #fff;
  font-size: 20px;
  line-height: 150px;
  text-align: center;
  background-color: #39a9ed;
  width: 395px;
  height: 395px;
  // position: relative;
  img {
    width: 100%;
    height: 395px;
    // position: absolute;
  }
}
.header-box {
  // overflow: hidden;
  // height: auto;
  .header {
    transition: 0.5s;
    opacity: 0;
    height: 90px;
    background-color: #eae9e5;
    width: 100%;
    position: fixed;
    top: 0;
    z-index: 999;
    display: flex;
    .return {
      width: 28px;
      height: 28px;
      position: absolute;
      left: 20px;
      top: 40px;
    }
    .title {
      width: 70%;
      height: 40px;
      position: absolute;
      top: 35px;
      left: 70px;
      // background-color: #000;
      display: flex;
      justify-content: space-around;
      div {
        line-height: 40px;
        font-size: 16px;
        justify-content: space-around;
        width: 60px;
        text-align: center;
        border-bottom: 1px solid transparent;
        transition: 0.5s;
      }
      .selected {
        transition: 0.5s;
        border-bottom: 1px solid black;
      }
    }
  }
}

.goods-detail {
  padding: 10px 20px;
  .spand {
    height: 45px;
    display: flex;
    .spand-money {
      line-height: 45px;
      font-size: 22px;
      text-align: left;
      font-weight: 700;
    }
    .spand-msg {
      height: 30px;
      color: $main;
      padding: 0 20px;
      border: 1px solid $main;
      margin-top: 5px;
      line-height: 25px;
      margin-left: 10px;
      border-radius: 5px;
      font-size: 14px;
    }
  }
  .revenue {
    height: 22px;
    font-size: 12px;
    text-align: left;
    .revenue-msg {
      border-radius: 2px;
      font-weight: 700;
      background-color: $main;
      color: white;
      padding: 2px;
    }
    .revenue-spand {
      color: $main;
    }
  }
  .goods-msg {
    box-sizing: content-box;
    padding: 5px 0;
    font-size: 14px;
    color: black;
    text-align: left;
    font-weight: 700;
  }
  .goods-nice {
    text-align: left;
    font-weight: 700;
    color: $main;
  }
}
.yun {
  padding: 20px;
  .p {
    display: flex;
    line-height: 40px;
    font-size: 14px;

    .left {
      text-align: left;
      width: 30%;
      font-weight: 700;
    }
    .right {
      width: 70%;
      text-align: left;
      font-weight: 700;
    }
  }
}
.brand {
  padding: 10px 20px;
  .title {
    text-align: left;
    font-size: 16px;
    font-weight: 700;
    margin-bottom: 10px;
  }
  .p {
    display: flex;
    line-height: 40px;
    font-size: 14px;

    .left {
      text-align: left;
      width: 30%;
      font-weight: 700;
    }
    .right {
      width: 70%;
      text-align: left;
      font-weight: 700;
    }
  }
}
.goods-detaiByImg {
  padding: 10px 20px;
  .title {
    text-align: left;
    font-size: 16px;
    font-weight: 700;
    margin-bottom: 10px;
  }
  .img-box {
    width: 100%;
    // height: auto;
    img {
      width: 100%;
      height: auto;
    }
  }
}
.foryoutitle {
  background-color: $main;
  line-height: 40px;
  font-size: 18px;
  font-weight: 700;
  text-align: left;
  padding-left: 20px;
  padding-top: 10px;
}
.foryou-content {
  background-color: $main;
  padding: 10px;
  // margin-bottom: 100px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  .foryou-item {
    background-color: #fff;
    width: 48%;
    margin-top: 15px;
    // width: 165px;
    // height: 260px;
    position: relative;
    box-shadow: 0px 2px 10px rgba(0, 0, 0, 0.349019607843137);

    .msg {
      padding: 0 10px;
      font-size: 16px;
      text-align: left;
    }
    .price {
      font-size: 16px;
      font-weight: 700;
      color: #c1ab96;
      text-align: left;
      margin-top: 10px;
      padding: 10px 10px;
      position: relative;
    }
    .imgbox {
      width: 100%;
      height: auto;
      // background-color: red;
      overflow: hidden;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
}
.tofooter {
  text-align: center;
  line-height: 36px;
  font-size: 16px;
  font-weight: 700;
  color: $main;
  background-color: #eae9e5;
}
.footer {
  height: 50px;
}
//
</style>
<script>
// import homeModel from "../model/homeModel";
import homeModel from "../model/homeModel";
// import usermodel from "../model/userModel";
import shoppingcartModel from "../model/shoppingcartModel";
export default {
  data() {
    return {
      cartnum: 0,
      alllist: [],
      bannerlist: [],
      goodslist: [],
      detaillist: [],
      titlelist: ["商品", "详情", "推荐"],
      active: 0,
      revenue: 0,
    };
  },
  methods: {
    tohome() {
      this.$router.history.push("/");
    },
    addcart() {
      // console.log(this.goodslist);
      if (!localStorage.getItem("username")) {
        this.$router.history.push("/login");
      } else {
        let username = localStorage.getItem("username");
        // console.log(this.goodslist);
        let { id: goodsid, goodmsg, goodprice, src } = this.goodslist;
        // console.log(this.goodslist.id);
        let info = {
          username,
          goodsid,
          goodmsg,
          goodprice,
          src,
          buynum: 1,
          ischecked: false,
          isbuy: false,
        };

        //判断用户是否购买
        shoppingcartModel.query({ goodsid, username }).then((res) => {
          let list = res.data;
          // console.log(111, list);
          if (list.length) {
            let id = list[0].id;
        
            // console.log(id);
            shoppingcartModel.updata(id, { buynum: list[0].buynum + 1 });
            this.$toast.success("加入购物车成功");
          } else {
            shoppingcartModel.add(info).then(() => {
              // console.log(res.data);
              this.$toast.success("加入购物车成功");
              this.cartnum++
            });
          }
        });
      }
    },
    toShoppingcart() {
      this.$router.history.push("/shoppingcart");
    },
    getDetail(id) {
      // console.log(aa);
      this.$router.history.push({
        name: "detail",
        params: { id },
      });
      this.$router.go(0);
    },
    goreturn() {
      this.$router.go(-1);
    },
  },
  created() {
    //进入页面导航隐
    this.$store.commit("changeNavFlag", false);
    //

  shoppingcartModel.query({username:localStorage.getItem("username")}).then(res=>{
    console.log(res.data.length)
    this.cartnum=res.data.length+1
  })

    homeModel.query({ id: this.$route.params.id }).then((res) => {
      this.goodslist = res.data[0];
      this.bannerlist = this.goodslist.bannerlist;
      this.detaillist = this.goodslist.detaillist;
      this.classify = this.goodslist.classify;
      // console.log(this.goodslist);
      // console.log(this.bannerlist);
      // console.log(this.detaillist);

      homeModel.query({ classify: this.goodslist.classify }).then((res) => {
        this.alllist = res.data;
        // console.log(this.alllist);
      });

      this.revenue = (this.goodslist.goodprice * 0.03).toFixed(2);
    });
  },
  beforeDestroy() {
    this.$store.commit("changeNavFlag", true);
  },
  mounted() {
    var that = this;
    window.onscroll = function () {
      // console.log("滚动条滚动时触发此事件");
      // 滚动条距离页面顶部的位置  ==  页面上升的距离是等价的
      var header = document.querySelector(".header-box.header");
      var scrollTop = document.documentElement.scrollTop;
      if (scrollTop >= 200) {
        header.style.opacity = "1";
      } else {
        header.style.opacity = "0";
      }

      if (scrollTop >= 2400) {
        that.active = 1;
        if (scrollTop >= 5000) {
          that.active = 2;
        }
      } else {
        that.active = 0;
      }
    };
  },
};
</script>